<?
// PHPCLASS STUFF
$HEAD=<<<END
/**
* $ppd
* @link $projLink
* @copyright Copyright: $projCompany
* @author $projAuthor
* @access public
* @package $projName
*/
/**
* MODULE: $modName
* $modDesc	
* @package $projName
* @version $projVersion
* @uses DM
*/
class $className extends DM{\n
END;
$keyItem = "";
foreach($mod->values->vars as $myItem){
	$HEAD .="\t/**"."\n";
	$HEAD .="\t* ".$myItem->description."\n";
	$HEAD .="\t*@var ".$myItem->type."\n";
	$HEAD .="\t*/"."\n";
	$HEAD .="\tvar \$".$myItem->name.";\n";
	if ($myItem->index=="p"||$myItem->index=="pk"||$myItem->index=="primary"||$myItem->index=="primary key")
		$keyItem=$myItem->name;
}
$HEAD .="\t/**"."\n";
$HEAD .="\t* Toggle debugging state for object\n";
$HEAD .="\t*@var boolean/bit\n";
$HEAD .="\t*/"."\n";
$HEAD .= "\tvar \$debugging=false;\n";
$HEAD .="\t/**"."\n";
$HEAD .="\t* List where SQL resultSet will be stored\n";
$HEAD .="\t*@var array\n";
$HEAD .="\t*/"."\n";
$HEAD .= "\tvar \$list;\n";
$HEAD .= "\n";


$HEAD .= "\t/**\n";
$HEAD .= "\t*	Constructor class.\n";
$HEAD .= "\t*	@return Stirng\n";
$HEAD .= "\t*/\n";
$HEAD .= "\tfunction $className(){\n";
$HEAD .= "\t\t//LOAD THE DATABSE CLASS\n";
$HEAD .= "\t\t\$this->loadClass('database','database');\n";
$HEAD .= "\t\t//INITIALIZE THE VARIABLES\n";
foreach($mod->values->vars as $myItem){
	$HEAD .= "\t\t\$this->".$myItem->name." = '';\n";
}
$HEAD .= "\t\t\$this->lista = array();\n";
$HEAD .= "\t\t\$this->debugging=false;\n";
$HEAD .= "\t}\n";

// FETCH SINGLE

$HEAD .= "\t/**\n";
$HEAD .= "\t*	Fetch a single object using the key.\n";
$HEAD .= "\t*	@return Stirng\n";
$HEAD .= "\t*/\n";
$HEAD .= "\tfunction get(){\n";
$HEAD .= "\t\t//SQL STRING\n";
$HEAD .= "\t\t\$sql=\"SELECT * FROM $tp".$mod->name." ".($keyItem==""?"":"WHERE $keyItem='\$this->$keyItem'")."\";\n";
$HEAD .= "\t\t\$db= new database();\n";
$HEAD .= "\t\t\$db->debugging=\$this->debugging;\n";
$HEAD .= "\t\tif (\$db->connect()){\n";
$HEAD .= "\t\t\tif (\$db->fetchSingle(\$sql)){\n";
foreach($mod->values->vars as $myItem){
	$HEAD .= "\t\t\t\t\$this->".$myItem->name."=\$db->resultArray['".$myItem->name."'];\n";
}
$HEAD .= "\t\t\t\treturn \$this->watch(true,\$db,'get_single');\n";
$HEAD .= "\t\t\t}else{\n";
$HEAD .= "\t\t\t\t\$this->errorReport('Error in the select',\$sql);\n";
$HEAD .= "\t\t\t\treturn \$this->watch(false,\$db,\"get_List - \$sql\");\n";
$HEAD .= "\t\t\t}\n";
$HEAD .= "\t\t}else{\n";
$HEAD .= "\t\t\t\$this->errorReport('Could not conncet to the database',\$db);\n";
$HEAD .= "\t\t\treturn \$this->watch(false,\$db,'get_single');\n";
$HEAD .= "\t\t}\n";
$HEAD .= "\t}\n";

// FETCH LIST

$HEAD .= "\t/**\n";
$HEAD .= "\t*	Get the list of objects from the DB.\n";
$HEAD .= "\t*	@return Stirng\n";
$HEAD .= "\t*/\n";
$HEAD .= "\t function getList(){\n";
$HEAD .= "\t\t//SQL STRING\n";
$HEAD .= "\t\t\$sql=\"SELECT * FROM $tp".$mod->name."\";\n";
$HEAD .= "\t\t\$db= new database();\n";
$HEAD .= "\t\t\$db->debugging=\$this->debugging;\n";
$HEAD .= "\t\tif (\$db->connect()){\n";
$HEAD .= "\t\t\tif (\$db->fetch(\$sql)){\n";
$HEAD .= "\t\t\t\t\$this->lista=\$db->resultArray;\n";
$HEAD .= "\t\t\t\treturn \$this->watch(true,\$db,'get_list');\n";
$HEAD .= "\t\t\t}else{\n";
$HEAD .= "\t\t\t\t\$this->errorReport('Error in the select',\$sql);\n";
$HEAD .= "\t\t\t\treturn \$this->watch(false,\$db,\"get_List - \$sql\");\n";
$HEAD .= "\t\t\t}\n";
$HEAD .= "\t\t}else{\n";
$HEAD .= "\t\t\t\$this->errorReport('Could not conncet to the database',\$db);\n";
$HEAD .= "\t\t\treturn \$this->watch(false,\$db,'get_list');\n";
$HEAD .= "\t\t}\n";
$HEAD .= "\t}\n";

// SAVE

$HEAD .= "\t/**\n";
$HEAD .= "\t*	Save current Object (update if key is not Null).\n";
$HEAD .= "\t*	@return boolean\n";
$HEAD .= "\t*/\n";
$HEAD .= "\t function save(){\n";
$insertCols="";
$insertVals="";
$updates="";
foreach($mod->values->vars as $myItem){
	$insertCols.="\t\t\t\t".$myItem->name.", \n";
	$insertVals.="\t\t\t\t"."'\".\$this->".$myItem->name.".\"', \n";
	$updates .= "\t\t\t\t".$myItem->name."="."'\".\$this->".$myItem->name.".\"', \n";
}
$insertCols=rtrim($insertCols,", \n");
$insertVals=rtrim($insertVals,", \n");
$updates=rtrim($updates,", \n");

if ($keyItem==""){
	// WE ONLY HAVE AN INSERT DUE TO LACK OF PKs
	$HEAD .= "\t\t//SQL STRING - WE HAVE NO KEYS SET - ONLY INSERT WILL BE USED\n";
	$HEAD .= "\t\t\$sql=\"INSERT INTO $tp".$mod->name."(\n$insertCols\n\t\t\t)VALUES(\n$insertVals\n\t\t\t)\";\n";
}else{
	$HEAD .= "\t\t//SQL STRING - CHECK KEY TO SEE IF WE USE INSERT OR UPDATE\n";
	$HEAD .= "\t\tif (\$this->$keyItem==''){\n";
	$HEAD .= "\t\t\t\$sql=\"INSERT INTO $tp".$mod->name."(\n$insertCols\n\t\t\t)VALUES(\n$insertVals\n\t\t\t)\";\n";
	$HEAD .= "\t\t}else{\n";
	$HEAD .= "\t\t\t\$sql=\"UPDATE $tp".$mod->name." SET \n$updates\n\t\t\t\";\n";
	$HEAD .= "\t\t}\n";
}
$HEAD .= "\t\t\$db= new database();\n";
$HEAD .= "\t\t\$db->debugging=\$this->debugging;\n";
$HEAD .= "\t\tif (\$db->connect()){\n";
$HEAD .= "\t\t\tif (\$db->execute(\$sql)){\n";
$HEAD .= "\t\t\t\tif (\$this->$keyItem=='') \$this->$keyItem=\$db->lastInsertID(); \n";
$HEAD .= "\t\t\t\treturn \$this->watch(true,\$db,'save_item');\n";
$HEAD .= "\t\t\t}else{\n";
$HEAD .= "\t\t\t\t\$this->errorReport('Error in the select',\$sql);\n";
$HEAD .= "\t\t\t\treturn \$this->watch(false,\$db,\"save_item - \$sql\");\n";
$HEAD .= "\t\t\t}\n";
$HEAD .= "\t\t}else{\n";
$HEAD .= "\t\t\t\$this->errorReport('Could not conncet to the database',\$db);\n";
$HEAD .= "\t\t\treturn \$this->watch(false,\$db,'save_item');\n";
$HEAD .= "\t\t}\n";
$HEAD .= "\t}\n";

// DELETE

$HEAD .= "\t/**\n";
$HEAD .= "\t*	Delete element from DB using primary key.\n";
$HEAD .= "\t*	@return boolean\n";
$HEAD .= "\t*/\n";
$HEAD .= "\t function delete(){\n";
$HEAD .= "\t\t//SQL STRING\n";
$HEAD .= "\t\t\$sql=\"DELETE FROM $tp".$mod->name." ".($keyItem==""?"":"WHERE $keyItem='\$this->$keyItem'")."\";\n";
$HEAD .= "\t\t\$db= new database();\n";
$HEAD .= "\t\t\$db->debugging=\$this->debugging;\n";
$HEAD .= "\t\tif (\$db->connect()){\n";
$HEAD .= "\t\t\tif (\$db->execute(\$sql)){\n";
$HEAD .= "\t\t\t\treturn \$this->watch(true,\$db,'delete_item');\n";
$HEAD .= "\t\t\t}else{\n";
$HEAD .= "\t\t\t\t\$this->errorReport('Error in the select',\$sql);\n";
$HEAD .= "\t\t\t\treturn \$this->watch(false,\$db,\"delete_item - \$sql\");\n";
$HEAD .= "\t\t\t}\n";
$HEAD .= "\t\t}else{\n";
$HEAD .= "\t\t\t\$this->errorReport('Could not conncet to the database',\$db);\n";
$HEAD .= "\t\t\treturn \$this->watch(false,\$db,'delete_item');\n";
$HEAD .= "\t\t}\n";
$HEAD .= "\t}\n";

if ($currentConfig->mysql_tables){
	$HEAD .= "\t/**\n";
	$HEAD .= "\t*	Setup Mysql Table.\n";
	$HEAD .= "\t*	@return Boolean\n";
	$HEAD .= "\t*/\n";
	$HEAD .= "\t function fw_setup(){\n";
	$HEAD .= "\t\t//SQL STRING\n";
	$HEAD .= "\t\t\$sql=\"".trim(rtrim(str_replace("\n","\n\t\t\t",$myMysql),"\n\t\t\t"))."\";\n";
	$HEAD .= "\t\t\$db= new database();\n";
	$HEAD .= "\t\t\$db->debugging=\$this->debugging;\n";
	$HEAD .= "\t\tif (\$db->connect()){\n";
	$HEAD .= "\t\t\tif (\$db->execute(\$sql)){\n";
	$HEAD .= "\t\t\t\treturn \$this->watch(true,\$db,'setup_table');\n";
	$HEAD .= "\t\t\t}else{\n";
	$HEAD .= "\t\t\t\t\$this->errorReport('Error in the select',\$sql);\n";
	$HEAD .= "\t\t\t\treturn \$this->watch(false,\$db,\"setup_table - \$sql\");\n";
	$HEAD .= "\t\t\t}\n";
	$HEAD .= "\t\t}else{\n";
	$HEAD .= "\t\t\t\$this->errorReport('Could not conncet to the database',\$db);\n";
	$HEAD .= "\t\t\treturn \$this->watch(false,\$db,'setup_table');\n";
	$HEAD .= "\t\t}\n";
	$HEAD .= "\t}\n";
}

$HEAD .= "}\n\n";

echo $HEAD;
// END PHPCLASS STUFF
?>